<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<view>
			<u--form ref="queryForm" :model="query">
			  <u-form-item label="标准箱码" prop="containerBarCode" labelWidth="140rpx">
			    <u--input 
			    	placeholder="请输入/扫描" 
			    	v-model.trim="query.containerBarCode" 
			    	suffixIcon="scan" 
			    	suffixIconStyle="font-size: 68rpx"
			    	style="flex: 1;"
			    >
			    </u--input>
			  </u-form-item>
				<u-form-item label="物料码" prop="labelNo" labelWidth="140rpx">
				  <u--input 
				  	placeholder="请输入/扫描" 
				  	v-model.trim="query.labelNo" 
				  	suffixIcon="scan" 
				  	suffixIconStyle="font-size: 68rpx"
				  	style="flex: 1;"
						@confirm="extraInfo"
				  >
				  </u--input>
				</u-form-item>
			</u--form>
		</view>
		<view v-if="inBoundOrder" style="font-weight: 600;">入库单号: {{ inBoundOrder }}</view>
		<uni-table ref="info" border stripe emptyText="暂无数据" style="margin: 10px 0">
			<uni-tr>
				<uni-th width="90">料号</uni-th>
				<uni-th width="90">批次号</uni-th>
				<uni-th width="90">待入库数量</uni-th>
				<uni-th width="90">已绑定数量</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in inBoundList" :key="index" class="td-container">
				<uni-td width="90">{{ item.materialCode }}</uni-td>
				<uni-td width="90">{{ item.materialBatchNo }}</uni-td>
				<uni-td width="90">{{ item.materialQty }}</uni-td>
				<uni-td width="90">{{ item.totalQty }}</uni-td>
			</uni-tr>
		</uni-table>
		<!-- 提示组件 -->
		<u-toast ref="uToast"></u-toast>
		<view class="commonButton">
		  <u-button type="primary" :disabled="Object.keys(scanMaterial).length === 0 || !query.containerBarCode" @click="bind">确认绑定</u-button>
		  <!-- <u-button type="error" @click="Unbind">解绑容器</u-button> -->
		</view>
	</view>
</template>

<script>
	import { bindAPI, queryOrderAPI, returnInboundAPI } from '@/api/work/inbound.js'
	import {
		uniCard,
		uniTable,
		uniTr,
		uniTd,
		uniTh
	} from '@dcloudio/uni-ui'
	import uniDataSelect from '../../../node_modules/@dcloudio/uni-ui/lib/uni-data-select/uni-data-select.vue'
	export default {
		components: {
			uniCard,
			uniTable,
			uniTr,
			uniTd,
			uniTh,
			uniDataSelect
		},
		data() {
			return {
				query: {
					containerBarCode: '',
					labelNo: ''
				},
				inBoundOrder: '',
				inBoundList: [],
				scanMaterial: {}
			}
		},
		methods: {
			reset() {
				this.query = {
					containerBarCode: '',
					labelNo: ''
				}
			},
			async bind() {
				const res = await returnInboundAPI(this.query)
				if (res.code === 200) {
					this.inBoundList = res.data
					this.inBoundOrder = res.data[0].inBoundCode
					// this.reset()
					this.query.labelNo = ''
					this.scanMaterial = {}
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			Unbind() {},
			change(e) {
				this.reset()
			},
			extraInfo() {
				if (!this.query.labelNo.includes('*')) {
					this.query.labelNo = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				const array = this.query.labelNo.split('*')
				if (array.length !== 6) {
					this.query.labelNo = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				// 物料码 批次 日期 供应商 数量 reelId
				this.scanMaterial = {
					materialCode: array[0],
					materialBatch: array[1],
					date: array[2],
					supplier: array[3],
					materialQty: Number(array[4]),
					reelId: array[5]
				}
				this.query.labelNo = this.scanMaterial.reelId
			}
		}
	}
</script>

<style lang="scss">
.commonButton {
	width: 100%;
	display: flex;
	gap: 20rpx;
	position: fixed;
	left: 0;
	bottom: 0;
	box-sizing: border-box;
	padding: 15rpx;
	justify-content: space-between;
}
</style>
